package com.flydog.taskManager.common.converter;

import com.flydog.taskManager.dto.RepeatMode;
import org.springframework.stereotype.Component;

import javax.persistence.AttributeConverter;
import javax.persistence.Converter;

/**
 * 重复模式转换类
 *
 * @author caoweidong
 * @version 0.0.0
 * @create 2021/6/18 8:12
 */

@Converter
@Component
public class RepeatModeConverter implements AttributeConverter<RepeatMode, Integer> {
    @Override
    public Integer convertToDatabaseColumn(RepeatMode repeatMode) {
        return repeatMode.getIndex();
    }

    @Override
    public RepeatMode convertToEntityAttribute(Integer integer) {
        if (integer != null) {
            for (RepeatMode mode: RepeatMode.values()) {
                if(mode.getIndex() == integer) {
                    return mode;
                }
            }
        }
        return null;
    }
}
